package com.cad3d.filemanager.action;

import java.io.File;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import org.apache.struts2.ServletActionContext;

import com.cad3d.*;
import com.cad3d.filemanager.dao.*;
import com.cad3d.filemanager.dbutils.*;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author bau
 * 
 */
public class FileUploadAction extends ActionSupport implements
		ServletRequestAware {

	private File userImage;
	private String userImageContentType;
	private String userImageFileName;
	String serverFilePath;
	private long fileSize;
	private Logger log = Logger.getLogger(FileUploadAction.class);
	private boolean filePublic;

	public boolean isFilePublic() {
		return filePublic;
	}

	public void setFilePublic(boolean filePublic) {
		this.filePublic = filePublic;
	}

	private HttpServletRequest servletRequest;

	public String execute() {
		User user = (User) ServletActionContext.getRequest().getSession()
				.getAttribute("loggedInUser");
		String username = user.getUsername();
		try {
			serverFilePath = servletRequest.getSession().getServletContext()
					.getRealPath("/");

			serverFilePath = serverFilePath + "upload/" + username;
			log.debug("Server path:" + serverFilePath);

			File fileToCreate = new File(serverFilePath, this.userImageFileName);

			FileUtils.copyFile(this.userImage, fileToCreate);

		} catch (Exception e) {
			e.printStackTrace();
			addActionError(e.getMessage());

			return INPUT;
		}

		log.debug("File upload: " + getUserImageFileName());
		log.debug("File size: " + this.getFileSize());

		return doAddFile();

	}

	public String doAddFile() {

		List<Filemanagement> listFilefullname = new ArrayList<Filemanagement>();
		listFilefullname = FileManagementTableTool.getInstance()
				.findFileByFullFilename(getUserImageFileName());
		if (listFilefullname.size() > 0) {
			return "fail";
		}
		User user = (User) ServletActionContext.getRequest().getSession()
				.getAttribute("loggedInUser");

		this.fileSize = userImage.length();

		log.debug("\nRunning doAddFile() Function!!!\n");

		Filemanagement aFile = new Filemanagement();
		aFile.setUser(user);
		String filename = getUserImageFileName();
		aFile.setFileName(filename);

		String fileP = servletRequest.getSession().getServletContext()
				.getRealPath("/");
		fileP = fileP + "upload" + File.separator + user.getUsername()
				+ File.separator + aFile.getFileName();
		aFile.setFilePath("upload/" + user.getUsername() + "/" + aFile.getFileName());

		String fileExtension[] = getUserImageFileName().split("\\.");
		String extension = fileExtension[fileExtension.length - 1];

		aFile.setFileType(extension);

		int size = (int) (this.getFileSize());
		aFile.setFileSize(size);

		Date aNow = new Date();
		aFile.setFileUploadDate(aNow);

		aFile.setDownloadTime(0);
		aFile.setThumbnailPath(" ");

		aFile.setAbsolutePath(fileP);
		aFile.setFilePublic(filePublic);

		if (FileManagementTableTool.getInstance().addFile(aFile)) {
			return "success";
		} else {
			return "fail";
		}
	}

	public File getUserImage() {

		return userImage;

	}

	public void setUserImage(File userImage) {

		this.userImage = userImage;

	}

	public String getUserImageContentType() {

		return userImageContentType;

	}

	public void setUserImageContentType(String userImageContentType) {

		this.userImageContentType = userImageContentType;

	}

	public String getUserImageFileName() {

		return userImageFileName;

	}

	public void setUserImageFileName(String userImageFileName) {

		this.userImageFileName = userImageFileName;

	}

	public long getFileSize() {
		return fileSize;
	}

	public void setFileSize(long aFilesize) {
		this.fileSize = aFilesize;
	}

	@Override
	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;

	}
}